cmake_minimum_required(VERSION 3.10)

project(enrigin_gst)

find_package(PkgConfig REQUIRED)
# Query GStreamer modules via pkg-config.
# NOTE: We intentionally avoid IMPORTED_TARGET here because on some
# systems the generated PkgConfig::GST target contains broken
# INTERFACE_INCLUDE_DIRECTORIES (e.g. x86_64 glib-2.0 paths on
# aarch64 rootfs), which makes CMake configuration fail.
pkg_check_modules(GST REQUIRED gstreamer-1.0)
pkg_check_modules(GST_BASE REQUIRED gstreamer-base-1.0)
pkg_check_modules(GST_VIDEO REQUIRED gstreamer-video-1.0)

# Make CMake aware of custom find modules (for RivsFFmpeg)
# We reuse the Rivs FFmpeg find-module from the sibling videopipe project.
list(APPEND CMAKE_MODULE_PATH
    "${CMAKE_CURRENT_LIST_DIR}/otl/common/cmake"
    )
find_package(RivsFFmpeg REQUIRED)

# Optional: TopsInference (not required for enrigin plugins)
find_path(TOPSINFERENCE_INCLUDE_DIR TopsInference/TopsInferRuntime.h
        PATHS /opt/rivs/include $ENV{HOME}/enrigin_sdk/usr/include $ENV{HOME}/buildroot/include
        DOC "TopsInference include directory")
find_path(TOPS_EXT_INCLUDE_DIR tops/tops_ext.h
        PATHS /opt/rivs/include $ENV{HOME}/enrigin_sdk/opt/rivs/include $ENV{HOME}/buildroot/include
        DOC "Tops include directory")

if (NOT TOPSINFERENCE_INCLUDE_DIR)
    set(TOPSINFERENCE_INCLUDE_DIR "")
endif()
if (NOT TOPS_EXT_INCLUDE_DIR)
    set(TOPS_EXT_INCLUDE_DIR "")
endif()

# GStreamer plugin: gst-enriginenc (Direct FFmpeg Implementation)
# This plugin uses FFmpeg VSV encoder directly without OTL dependency
add_library(gstenriginenc SHARED gst-enriginenc.cpp)
target_include_directories(gstenriginenc PRIVATE 
    ${TOPSINFERENCE_INCLUDE_DIR}
    ${TOPS_EXT_INCLUDE_DIR}
    ${GST_INCLUDE_DIRS}
    ${GST_BASE_INCLUDE_DIRS}
    ${GST_VIDEO_INCLUDE_DIRS})
target_link_libraries(gstenriginenc
		${GST_LIBRARIES}
		${GST_BASE_LIBRARIES}
		${GST_VIDEO_LIBRARIES}
		Rivs::FFMPEG)

# Example program for enriginenc
add_executable(example_enriginenc example_enriginenc.cpp)
target_include_directories(example_enriginenc PRIVATE ${GST_INCLUDE_DIRS})
target_link_libraries(example_enriginenc ${GST_LIBRARIES})

message(STATUS "enriginenc plugin will be built with Rivs::FFMPEG")

# GStreamer plugin: gst-enrigindec (Direct FFmpeg Decoder)
add_library(gstenrigindec SHARED gst-enrigindec.cpp)
target_include_directories(gstenrigindec PRIVATE 
	${TOPSINFERENCE_INCLUDE_DIR}
	${TOPS_EXT_INCLUDE_DIR}
	${GST_INCLUDE_DIRS}
	${GST_BASE_INCLUDE_DIRS}
	${GST_VIDEO_INCLUDE_DIRS})
target_link_libraries(gstenrigindec
		${GST_LIBRARIES}
		${GST_BASE_LIBRARIES}
		${GST_VIDEO_LIBRARIES}
		Rivs::FFMPEG)

message(STATUS "enrigindec plugin will be built with Rivs::FFMPEG")
